﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using HouseAgencySite.ViewModel;

namespace HouseAgencySite.Areas.Admin.Controllers
{    
    public class CheckLogin : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Session != null)
            {
                //if (HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName] == null)
                //{
                //    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { Controller = "Account", Action = "Login" }));
                //}
                if (filterContext.HttpContext.Session.IsNewSession)
                {
                    if (filterContext.HttpContext.Session["UserInfo"] == null)
                    {
                        filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { Controller = "Account", Action = "Login" }));
                    }
                    else
                    {
                        LogUserInfo user = filterContext.HttpContext.Session["UserInfo"] as LogUserInfo;
                        if (user == null || string.IsNullOrEmpty(user.Guid))
                        {
                            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { Controller = "Account", Action = "Login" }));
                        }
                    }
                }
            }

        }
    }
}
